package com.ruoyi.web.controller.monitor;

import java.util.*;

import com.ruoyi.material.service.IBaseFileLabelService;
import com.ruoyi.material.service.IBaseMaterialService;
import com.ruoyi.material.service.IBaseTagFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static com.ruoyi.material.util.MapKeyComparator.getDayMap;
import static com.ruoyi.material.util.MapKeyComparator.sortMapByKey;

/**
 * 缓存监控
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/monitor/count")
public class CountController
{

    @Autowired
    private IBaseMaterialService baseMaterialService;
    @Autowired
    private IBaseTagFileService baseTagFileService;
    @Autowired
    private IBaseFileLabelService baseFileLabelService;
    /**
     * 统计某种文件数量
     *
     * @param type 类型
     * @return 结果
     */
    @GetMapping("/countUncertainFiles")
    @ResponseBody
    public int countUncertainFiles(String type)
    {
        return baseMaterialService.countUncertainFiles(type);
    }
    /**
     * 统计文件数量
     *
     * @return 结果
     */
    @GetMapping("/countFiles")
    @ResponseBody
    public int countBaseMaterialFiles()
    {
        return baseMaterialService.countBaseMaterialFiles();
    }
    /**
     * 统计文件夹数量
     *
     * @return 结果
     */
    @GetMapping("/countFolders")
    @ResponseBody
    public int countBaseMaterialFolders()
    {
        return baseMaterialService.countBaseMaterialFolders();
    }
    /**
     * 按照日期统计 近30天 文件数量
     *
     * @return 结果
     */
    @GetMapping("/countFilesByDate")
    @ResponseBody
    public Map<String, String> countBaseMaterialFilesByDate(){
        //获取30天的日期集合
        Map<String, String> dayMap = getDayMap();//key为月-日，value为0
        List countRecord=baseMaterialService.countBaseMaterialFilesByDate();//create_time:****-**-**,num:个数
        if(countRecord.size()>0){
            for(int i=0;i<countRecord.size();i++){
                for(String k : dayMap.keySet()) {
                    String strDate = countRecord.get(i).toString().substring(18);
                    strDate = strDate.substring(0, strDate.indexOf(","));//取create_time的月日**—**
                    if (k.equals(strDate)){
                        String value = countRecord.get(i).toString().substring(29);
                        value=value.substring(0, value.indexOf("}"));//取num个数
                        dayMap.put(k,value);
                    }
                }
            }
        }
        dayMap=sortMapByKey(dayMap);	//按Key进行排序
        return dayMap;
    }
    /**
     *统计标签数量
     * @return 结果
     */
    @GetMapping("/countTags")
    @ResponseBody
    public int countTags(){
        return baseTagFileService.countTags();
    }

    /**
     *统计标签数量
     * @return 结果
     */
    @GetMapping("/countLabels")
    @ResponseBody
    public int countLabels(){
        return baseFileLabelService.countLabels();
    }
}
